package com.example.datacollection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.datacollection.bean.TableTest;
import com.example.datacollection.config.ResultMap;
import com.example.datacollection.mapper.TableMapper;
import com.example.datacollection.service.TableService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class TableServiceImpl extends ServiceImpl<TableMapper, TableTest> implements TableService {

    @Autowired
    private TableMapper tableMapper;

    @Override
    public IPage<TableTest> getTbleList(Map map) {
        LambdaQueryWrapper<TableTest> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(map.get("address").toString())) {
            wrapper.eq(TableTest::getAddress, map.get("address"));
        }
        if (StringUtils.isNotEmpty(map.get("name").toString())) {
            wrapper.like(TableTest::getName, map.get("name"));
        }
        Page<TableTest> page = new Page<>(Integer.parseInt(map.get("currentPage").toString()), Integer.parseInt(map.get("pageSize").toString()));
        IPage<TableTest> iPage = tableMapper.selectPage(page, wrapper);
        return iPage;
    }

    @Override
    public Map<String, Object> saveTable(TableTest tableTest) {
        String replace = UUID.randomUUID().toString().replace("-", "");
        tableTest.setId(replace);
        tableMapper.insert(tableTest);
        return ResultMap.ok();
    }
}
